<?php
namespace App\Services\WxCardMemberLevel;

use App\Models\WxCardMemberList;
use App\Services\WxCard\WxCardMemberCard;
use App\Services\WxMaterial\WxMaterialNews;

/**
 * 通用类会员卡-普通会员业务类
 * @package App\Services\WxCardMemberLevel
 */
class CommonCardNormal extends MemberLevelAbstract
{
    /**
     * 检查判断会员等级是否有效，并返回该等级对应的数据
     * @param $userOpenId
     * @return bool
     * @throws \Exception
     */
    public function checkValid($userOpenId)
    {
        $memberListModel = WxCardMemberList::where('openid', $userOpenId)->where('card_base_id', $this->levelData['card_base_id'])->first();
        $memberListData = $memberListModel ? $memberListModel->toArray() : [];
        if (!empty($memberListData)){
            $memberListData['user_info'] = (new WxCardMemberCard())->fomatUserInfo($memberListData['user_info']);
            //手机号存在则表示该会员为普通会 员
            if (!empty($memberListData['user_info']['mobile'])){
                //将该等级会员卡背景转换为微信地址
                if (!empty($this->levelData['background_pic_url'])){
                    $this->levelData['background_pic_url'] = (new WxMaterialNews())->getWxViewUrl($this->levelData['background_pic_url']);
                }
                $this->levelData['mobile'] = $memberListData['user_info']['mobile'];
                return $this->levelData;
            }
        }
        return false;
    }
}